package cn.itsource.crm.web.controller;

import cn.itsource.AjaxResult;
import cn.itsource.PageList;
import cn.itsource.crm.domain.Eunuch;
import cn.itsource.crm.query.EunuchQuery;
import cn.itsource.crm.service.IEunuchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/eunuch")
@Api(tags = "太监接口类") //给前端看的
@CrossOrigin//解决跨域问题
public class EunuchController {

    @Autowired
    private IEunuchService eunuchService;

    //分页查询方法
    @RequestMapping(value = "/queryPage",method = RequestMethod.PATCH)
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code=200,message="成功"),
            @ApiResponse(code=500,message="服务器错误")
    })
    @ApiOperation(value="分页查询太监",notes = "queryPage查询方法")
    @CrossOrigin
    public PageList queryPage(@RequestBody EunuchQuery eunuchQuery){
        return eunuchService.queryPage(eunuchQuery);
    }

    //获取太监列表 指定获取方式-path
    @RequestMapping(value = "/list",method = RequestMethod.PATCH)
    @ResponseBody//把对象转成json格式
    @ApiResponses({//给前端看的
            @ApiResponse(code=200,message="成功"),
            @ApiResponse(code=500,message="服务器错误")
    })
    @ApiOperation(value="查询所有太监",notes = "queryAll查询方法")//给前端看的
    @CrossOrigin//解决跨域问题
    public List<Eunuch> queryAll(){
        return eunuchService.queryAll();
    }

    //查询单个用户
    // /Eunuch/1
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    @ApiOperation(value="根据ID查询太监",notes = "queryOne查询方法")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id",value = "id参数",required = true,dataType = "Long",paramType = "path")
//    })
    @ResponseBody
    public Eunuch getOne(@PathVariable Long id){//@PathVariable 接收请求路径中占位符的值
        return  eunuchService.queryOne(id);
    }


    @RequestMapping(value = "/save",method = RequestMethod.PUT)
    @ResponseBody
    @ApiOperation(value="保存太监",notes = "save方法")
    public AjaxResult save(@RequestBody Eunuch eunuch){//接收前端json数据
        try {
            eunuchService.save(eunuch);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("保存失败");

        }
        return new AjaxResult();
    }
    //删除
    @RequestMapping(value = "{id}",method = RequestMethod.DELETE)
    @ResponseBody
    @CrossOrigin
    @ApiOperation(value="根据id删除部门方法",notes = "delete方法")
    public AjaxResult delete(@PathVariable Long id){
        try {
            eunuchService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("删除失败");
        }
        return new AjaxResult();
    }

    //修改
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value="修改部门方法",notes = "update方法")
    public AjaxResult update(@RequestBody  Eunuch eunuch){
        try {
            eunuchService.update(eunuch);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("修改失败");
        }
        return new AjaxResult();
    }

}
